import myAxios from '../../../utils/myAxios';
import { message } from 'antd';
import moment from 'moment';
export const tableDataAction = (data) => {
  return { type: 'getData', data }
}
export const tableCount = (data) => {
  return { type: 'count', data }
}
//异步action获取table数据
/* findTime: 0为客户列表，1为今日新增，2为本周新增，3为本月新增*/
/* sort : 0为不需要排序，1为按编号升序，2为按录入时间升序，3为按更新时间升序*/
/* sortType  : 1为升序，2为降序，默认传1为升序*/
export const AsyncTableDataAction = (page = 1, pageSize = 7, findTime = 0, sort = 0, sortType = 1, customerType = '', x, ordinaryInformation, advancedInformation) => {
  //  这里还要再加一个判断，自定义一个形参x  0：正常获取table数据 ，2：普通搜索获取table数据 3：高级搜索获取table数据
  // ordinaryInformation :普通搜索内容
  // advancedInformation :高级搜索的参数
  let reqUrl, reqMethod, obj
  if (x == 0) {
    reqMethod = 'get'
    reqUrl = '/customer/findCustomerInformation'
    obj = { params: { limit: pageSize, page, findTime, sort, sortType, customerType } }
  } else if (x == 2) {
    //普通搜索
    reqMethod = 'post'
    reqUrl = '/customer/findLikeCustomer'
    obj = { data: { type: 2, page, limit: pageSize, ordinaryInformation } }
  } else {
    //高级搜索
    reqMethod = 'post'
    reqUrl = '/customer/findLikeCustomer'
    obj = { data: { type: 3, page, limit: pageSize, ordinaryInformation, ...advancedInformation } }
  }
  return (dispatch) => {
    myAxios({
      method: reqMethod,
      url: reqUrl,
      ...obj
    })
      .then(res => {
        //总条数
        dispatch(tableCount(res.data.data.count))
        // console.log(res.data.data);
        let tempArr = []
        res.data.data.customer.forEach(item => {
          // console.log(item)
          let obj = {
            key: item.id,
            name: item.customerUsername,
            Contact: item.customerName,
            jobType: item.customerPosition.title,
            phone: item.phoneNumber,
            qqNumber: item.qqNumber,
            customerType: item.customerType.title,
            customerLevel: item.customerLevel.title,
            source: item.customerSource.title,
            time: moment(item.entryTime).format('YYYY/MM/DD'),
            email: item.email,
            employee: item.employee.name,
            /* 上面的字段做table展示，下面的不在table里展示，在详情页里再展示 */
            employeeInputName: item.employeeInputName,
            birthDay: moment(item.birthDay).format('YYYY/MM/DD'),
            customerIndustry: item.customerIndustry.title,
            // comSort: item.comSort.bigClass + '-' + item.comSort.smallClass,
            bigClass: item.comSort.bigClass,
            smallClass: item.comSort.smallClass,
            customerLocation: item.customerLocation.province + '-' + item.customerLocation.city + '-' + item.customerLocation.area,
            address: item.customerLocation.address,
            remarks: item.remarks,
            updateTime: moment(item.updateTime).format('YYYY/MM/DD'),
            postcode: item.postal,
            url: item.webSite
          }
          tempArr.push(obj)
        });
        //在调用一个同步action
        dispatch(tableDataAction(tempArr))
      })

  }
}